  //
//  MusicController.m
//  HB
//
//  Created by 孙智慧 on 16/1/11.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "MusicController.h"
#import "MusicCollectionCell.h"
#import "MusicNetWorkOperation.h"
#import "MusicListByMusicStyleController.h"

@implementation MusicController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _array = [[NSMutableArray alloc] init];
    
    //确定是水平滚动，还是垂直滚动  布局向导55.
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    flowLayout.minimumLineSpacing = 0.0;//行间距(最小值)
    
    flowLayout.minimumInteritemSpacing = 0.0;//item间距(最小值)
    
    [self.MusicStore setCollectionViewLayout:flowLayout];
    
    self.MusicStore.dataSource=self;
    
    self.MusicStore.delegate=self;
    
    [self.MusicStore setBackgroundColor:[UIColor clearColor]];
    
    //注册Cell，必须要有
    [self.MusicStore registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    
    [self loadMusic];
    
    RegisterNotify(NofifyMusicPlay, @selector(stopNavBtnRoation));
}

-(void) stopNavBtnRoation
{
    [self stopNavRightBtnRoation];
    [self.marqueeView stopM];
    
}
-(void) loadMusic
{
    operation= [[MusicNetWorkOperation alloc] initWithDelegate:self];
    
    NSString *URL= [FxGlobal getServiceUrl:@"music" andFuncName:@"musicstyle"];
    
    NSDictionary * dictInfo = @{@"url":URL};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
}

-(void) opSuccessEx:(id)data opinfo:(NSDictionary *)dictInfo
{
    operation= nil;
    
    //热播单曲
    [_array removeAllObjects];
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data2 = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if ([[data2 objectForKey:@"result"] isEqualToString:@"1"])
    {
        NSArray *arrayData= [data2 objectForKey:@"data"];
        
        for (NSInteger i=0; i<[arrayData count]; i++)
        {
            
            NSDictionary *dict= [arrayData objectAtIndex:i];
 
            [_array addObject:dict];
        }
        
        [self.MusicStore performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
}


#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_array count];
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger i= indexPath.row;
    
    static NSString * CellIdentifier = @"MusicCollectionCell";
    
    MusicCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    NSDictionary *dict= [_array objectAtIndex:i];
    
    NSString *path=  [dict objectForKey:@"path"];
    
    NSString *pathName=  [dict objectForKey:@"pathName"];
    
    NSString *mtId=[dict objectForKey:@"id"];
    
    cell.mtID=mtId;
    
    cell.dict= dict;
    
    [Common downLoadImageByURL:cell.imgView andURL:path andName:pathName];
    
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(IsiPhone6P)
    {
        return CGSizeMake(202, 202); //6s plus
    }
    if(IsiPhone6)
    {
        return CGSizeMake(182, 182); //6s
    }
    if (IsiPhone5)
    {
        return CGSizeMake(155, 155); //5s
    }
    return CGSizeMake(140, 140); //5s
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if([self musicPlay].isRoationing)
    {
        self.marqueeView.musicName= [self musicPlay ].playingMusic.name;
        
        [self.marqueeView startM];
        
    }
    else
    {
        [self.marqueeView stopM];
    }
}

-(void) openMusicList:(NSInteger)mtID andDict:(NSDictionary *)dict
{
 
    MusicListByMusicStyleController *viewController= [self.storyboard instantiateViewControllerWithIdentifier:@"MusicListByMusicStyleController"];
    
    [viewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    
    viewController->mtID=mtID;
    
    viewController->dict=dict;
    
    [self.view.window.rootViewController presentViewController:viewController animated:true completion:nil];
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MusicCollectionCell * cell =(MusicCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    [self openMusicList:cell.mtID.intValue andDict:cell.dict];
 
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
@end
